﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRasta.Codecs;
using OpenRasta.Configuration;
using OpenRasta.DI;
using OpenRasta.Pipeline.Contributors;
using OpenRasta.Web.UriDecorators;
using uREST.Core.Codecs;
using uREST.Core.Contributors;
using uREST.Core.Resources;
using uREST.Core.Services;
using uREST.Core.Handlers;

namespace uREST.Core
{
    public class OpenRastaConfig : IConfigurationSource
    {
        #region IConfigurationSource Members

        public void Configure()
        {
            using (OpenRastaConfiguration.Manual)
            {
                ResourceSpace.Uses.PipelineContributor<HttpMethodOverriderContributor>();
                ResourceSpace.Uses.PipelineContributor<LegacyHttpProtocolContributor>();
                ResourceSpace.Uses.UriDecorator<ContentTypeExtensionUriDecorator>();

                //ResourceSpace.Uses.CustomDependency<ICodec, JsonNetCodec>(DependencyLifetime.Singleton);
                //ResourceSpace.Uses.CustomDependency<IMediaTypeReader, JsonNetCodec>(DependencyLifetime.Singleton);
                //ResourceSpace.Uses.CustomDependency<IMediaTypeWriter, JsonNetCodec>(DependencyLifetime.Singleton);
                ResourceSpace.Uses.CustomDependency<JsonNetCodec, JsonNetCodec>(DependencyLifetime.Singleton);

                // Register root service handler
                ResourceSpace.Has.ResourcesOfType<List<EndPoint>>()
                    .AtUri(uRESTConfig.BaseUrl).Named("EndPointList")
                    .HandledBy<EndPointHandler>()
                    .TranscodedBy<JsonNetCodec>();

                // Register service handlers
                foreach (var service in ServiceFactory.Services)
                    service.RegisterHandlers();
            }
        }

        #endregion
    }
}
